package com.sl.au.service.impl;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sl.au.entity.BussinessInspection;
import com.sl.au.entity.User;
import com.sl.au.entity.BusinessCheckDetail;
import com.sl.au.entity.BussinessCheck;
import com.sl.au.repository.BussinessInspectionRepository;
import com.sl.au.repository.BussinessCheckRepository;
import com.sl.au.repository.BusinessCheckDetailRepository;
import com.sl.au.service.BussinessCheckService;

@Service
@Transactional
public class BussinessCheckServiceImpl implements BussinessCheckService {
	@Autowired
	private BussinessCheckRepository bussinessCheckRepository;
    @Autowired
    private BussinessInspectionRepository bussinessInspectionRepository;
    @Autowired
    private BusinessCheckDetailRepository businessCheckDetailRepository;
	@Override
	public BussinessCheckRepository getRepository()
	{ 
		return this.bussinessCheckRepository;
	}
	public BusinessCheckDetailRepository getDetailRepository()
	{
		return this.businessCheckDetailRepository;
	}
	public List<BussinessCheck> getPage(int start, int length, String key, String orderName, String dir) {

		Page<BussinessCheck> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "checkDate");
		pages = bussinessCheckRepository.findAll(pageable);
		return pages.getContent();
	}

	@Override
	public int getCount(String key) {

		if (!StringUtils.hasText(key))
			return bussinessCheckRepository.findAll().size();
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "checkDate");
		return bussinessCheckRepository.getCheckName(key, pageable).getContent().size();
	}

	@Override
	public List<BussinessCheck> getChecks(int start, int length, String key, String orderName, String dir) {

		Page<BussinessCheck> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "checkDate");
		pages = bussinessCheckRepository.getCheckName(key, pageable);
		return pages.getContent();
	}
	@Override
	public List<BusinessCheckDetail> getCheckDetailByCheckId(int start, int length,String id,String orderName)
	{
		Page<BusinessCheckDetail> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, orderName);
		pages = this.businessCheckDetailRepository.getCheckDetailByCheckId(id, pageable);
		return pages.getContent();
	}
	
	@Override
	public List<BussinessCheck> getCheckHistory(int start, int length,String id, User user,String orderName) {
		Page<BussinessCheck> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, orderName);
		pages = bussinessCheckRepository.getCheckHistory(id, user.getId(), pageable);
		return pages.getContent();
	}

	@Override
	public List<BussinessCheck> getCheckHistory(int start, int length,String id,String orderName) {
		Page<BussinessCheck> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, orderName);
		pages = bussinessCheckRepository.getCheckHistory(id, pageable);
		return pages.getContent();
	}
	@Override
	public List<BussinessInspection> getChecksByTime(int start, int length, String id, String orderName, String dir)
	{
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "inspectDate");
		Page<BussinessInspection>pages= this.bussinessInspectionRepository.getChecksByTime(id, DateTime.now().toDate(), pageable);
		return pages.getContent();
	}
	
	@Override
	public List<BussinessInspection> getChecksByTimeAndNeedCheck(int start, int length, String id, String orderName, String dir)
	{
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "inspectDate");
		Page<BussinessInspection>pages= this.bussinessInspectionRepository.getChecksByTimeAndNeedCheck(id, DateTime.now().toDate(), pageable);
		return pages.getContent();
	}
	
	@Override
	public List<BussinessInspection> getInspectionByCheckId(int start, int length, String id, String orderName)
	{
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, orderName);
		Page<BussinessInspection>pages= this.bussinessInspectionRepository.getInspectsByCheckId(id, pageable);
		return pages.getContent();
	}
	
	@Override
	public List<BussinessInspection> getChecksByHistory(int start, int length, String id, String orderName, String dir)
	{
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "inspectDate");
		Page<BussinessInspection>pages= this.bussinessInspectionRepository.getChecksByHistory(id, pageable);
		return pages.getContent();
	}

	@Override
	public void save(BussinessCheck bussinessCheck) {
		bussinessCheckRepository.save(bussinessCheck);

	}
	
	@Override
	public void saveAll(BussinessCheck bussinessCheck,List<BusinessCheckDetail> businessCheckDetail,List<BussinessInspection> bussinessInspection) {
		
		bussinessCheckRepository.save(bussinessCheck);
		businessCheckDetailRepository.save(businessCheckDetail);
		this.bussinessInspectionRepository.save(bussinessInspection);
	}

	@Override
	public void deleteById(String id) {
		bussinessCheckRepository.delete(bussinessCheckRepository.findOne(id));

	}

	@Override
	public BussinessCheck findById(String id) {

		return bussinessCheckRepository.findOne(id);
	}
}
